import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:search_people/utils/component/component.dart';
import 'package:search_people/utils/component/page_state/widgets/widgets.dart';

import '../../../../entrance/route/routes.dart';
import '../../../../utils/img_util.dart';
import '../../../../value/topcolors.dart';
import '../person_detail/models/person_base_model/data.dart';
import 'index.dart';
import 'widgets/widgets.dart';

class PersonPortraitPage extends GetView<PersonPortraitController> {
  const PersonPortraitPage({Key? key}) : super(key: key);

  List<PersonBaseModel> _getList() {
    if (controller.state.pTc.index == 0) {
      return controller.state.fellow;
    }

    if (controller.state.pTc.index == 1) {
      return controller.state.alumni;
    }

    return controller.state.colleague;
  }

  // 主视图
  Widget _buildView() {
    return _getList().isEmpty
        ? EmptyView()
        : ListView.separated(
            itemBuilder: (ctx, index) {
              var e = _getList()[index];
              return InkWell(
                onTap: () {
                  Get.toNamed(RouterPath.person_detail,
                      arguments: e.leaderId, preventDuplicates: true);
                },
                child: Container(
                  decoration: BoxDecoration(
                      color: Colors.white,
                      border: Border(bottom: BorderSide(color: TopColors.bg))),
                  padding:
                      EdgeInsets.symmetric(horizontal: 12.r, vertical: 10.r),
                  child: Row(
                    children: [
                      loadAvater(size: 40.r, url: e.image),
                      SizedBox(
                        width: 10.r,
                      ),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              e.leaderName ?? '',
                              style: TextStyle(
                                  color: TopColors.black_33, fontSize: 12.sp),
                            ),
                            Text(
                              e.currentPosition ?? '',
                              style: TextStyle(
                                  color: TopColors.black_99, fontSize: 11.sp),
                            ),
                          ],
                        ),
                      ),
                      Icon(
                        Icons.keyboard_arrow_right_rounded,
                        color: TopColors.main_color,
                      ),
                    ],
                  ),
                ),
              );
            },
            separatorBuilder: (ctx, index) {
              return Container(
                color: TopColors.bg,
                height: 1,
              );
            },
            itemCount: _getList().length);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PersonPortraitController>(
      builder: (_) {
        var state = controller.state;
        return Scaffold(
          appBar: detailAppBar(
            context,
            title: '人物图谱',
            actions: [
              _getList().isEmpty
                  ? const SizedBox()
                  : TextButton(
                      onPressed: () {
                        Get.toNamed(RouterPath.relation, parameters: {
                          'id': state.leaderId,
                          'type': state.pTc.index == 0
                              ? '老乡'
                              : state.pTc.index == 1
                                  ? '校友'
                                  : '同事'
                        });
                      },
                      child: Text(
                        '图谱',
                        style: TextStyle(
                          color: TopColors.black_33,
                          fontSize: 14.sp,
                        ),
                      ))
            ],
            bottom: PreferredSize(
              preferredSize: Size.fromHeight(40.r),
              child: TPTabBar(tabCtrl: state.pTc, tabTitles: state.pTitles),
            ),
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
